/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('../Poker/Poker');
var NN = require('../Poker/NN');

var PokerPlayer = require('../Poker/player').PokerPlayer;

var Const = require('./const');
var SubCode = require('../subCode');
var Rule = require('./rules');

exports.JXGDYPlayer = PokerPlayer.extend({
    _robDealer: -1,             // 抢庄状态，-1/0/1 未选择/不抢/抢庄
    _selectedCards: null,     // 选择的三张牌

    init: function (uid, msg) {
        this._super(uid, msg);

        this.boomSum = 0;
        this.winSum = 0;
        this.defeatSum = 0;
        this.maxScore = 0;

        this.roundBS = 1;
        this.lastPlayCards = -1;
    },
    reset: function () {
        this.roundBS = 0;
        this.lastPlayCards = -1;
    },


    getInitHandCardNeedNum: function () {
        if (this.isDealer()) {
            return 6;
        } else {
            return 5;
        }
    },
    //出牌
    playCards: function (cardsArray) {

        cardsArray.forEach(function (v) {
            this._handCards.delCard(v);
        }.bind(this));
    },
    //摸牌
    getCard: function () {
        var room = this.getRoom();
        var card = room.popCard();
        if (!card) {
            room.broadcast("onPlayerGetCard", {
                "playerIndex": this.getIndex(),
                "nums": 0,
                "turns": room.turns
            });
            this.send("onAddCard", {
                "cards": []
            });
            return false;
        }
        this.addNewCard(card);
        room.turns += 1;
        room.broadcast("onPlayerGetCard", {
            "playerIndex": this.getIndex(),
            "nums": 1,
            "turns": room.turns
        });
        this.send("onAddCard", {
            "cards": [card]
        });
        return true;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function () {
        var info = this._super();

        return info;
    },

    getReconnectInfo: function (isSelf) {
        var info = this._super(isSelf);
        if (isSelf) {
            info.handCards = this._cardObjToArray(this._handCards.getCards());
        }
        info.roundScore = this.getRoundScore();
        info.score = this.getScore();
        info.lastPlayCards = this.lastPlayCards;
        info.handCardsNum = this._handCards.getCardNum();
        return info;
    },

    getSettlementInfo: function () {
        var info = this._super();
        info.handCards = this._cardObjToArray(this._handCards.getCards());
        info.roundBS = this.roundBS;
        info.maxScore = this.maxScore;
        info.winSum = this.winSum;
        info.defeatSum = this.defeatSum;
        info.boomSum = this.boomSum;

        return info;
    },

    settlementPattern: function () {
        var room = this.getRoom();
        if (this.getIndex() == room.winnerIndex) {

            return;
        }


        var winner = room.getPlayerByIndex(room.winnerIndex);

        var arrayCards = this._cardObjToArray(this._handCards.getCards())
        if (arrayCards.every(function (v) {
            return v == 1 || v == 2 || v == 3;
        })) {
            this.addRoundScore(0);
            winner.addRoundScore(0);
            return;
        }

        console.log(room.winnerIndex);
        if (this.getIndex() != room.winnerIndex) {
            var BS = room.sumBS;
            var nums = this._handCards.getCardNum();

            this.roundBS = BS;
            if (nums == 5) {
                if (room.MSMode == "FBFD") {
                    if (room._BSMax != 0) {
                        this.roundBS *= 2;
                        if (this.roundBS >= room._BSMax) {
                            this.roundBS = room._BSMax;
                        }
                    } else {
                        this.roundBS *= 2;
                    }
                }
            }
            console.log(-nums * this.roundBS);
            this.addRoundScore(-nums * this.roundBS);
            winner.addRoundScore(nums * this.roundBS);
        }
    },

    settlement: function () {
        this._super();
        var room = this.getRoom();
        if (this.getIndex() == room.winnerIndex) {
            if (this.getRoundScore() > this.maxScore) {
                this.maxScore = this.getRoundScore();
            }
        }

    },

    onStartNewRound: function () {
        this._super();
        this.reset();
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    /******************************************************************
     * 私有功能函数
     ******************************************************************/

    _cardArrayToObj: function (arrayCards) {
        var obj = {};
        arrayCards.forEach(function (v) {
            if (obj[v]) {
                obj[v] += 1;
            } else {
                obj[v] = 1;
            }
        });
        return obj;
    },
    _cardObjToArray: function (arrayObj) {
        var keys = Object.keys(arrayObj);
        var temp = [];
        keys.forEach(function (v) {
            if (arrayObj[v]) {
                temp.push(v);
            }
        });
        return temp;
    },
    // getInitHandCardPreCards: function () {

    //     var preCards = [];
    //     if (this._prehandcards) {
    //         for (var i = 0; i < this._prehandcards.length; i++) {
    //             preCards.push(this._prehandcards[i]);
    //         }
    //     }
    //     var distance = this.getRoom().getIndexDistance(this.getIndex(), this.getRoom().getDealerIndex());
    //     console.log("distanceee")
    //     console.log(distance);
    //     if (distance == 0) {
    //         preCards.push(1);
    //         preCards.push(2);
    //         preCards.push(108);
    //         preCards.push(208)
    //         preCards.push(308)



    //     } else if (distance == 1 || distance == 4 || distance == 2) {
    //         preCards.push(3);
    //         // preCards.push(302);
    //         // preCards.push(402);
    //         // preCards.push(204);
    //         // preCards.push(207);
    //         // preCards.push(208);


    //     }
    //     //else if (distance == 2 || distance == 5) {
    //     // preCards.push(201);
    //     // preCards.push(202);
    //     // preCards.push(203);
    //     // preCards.push(204);
    //     // preCards.push(205);
    //     // 102,202,302,402,209
    //     //}
    //     var room = this.getRoom();
    //     logger.debug("room=%d noPreCards %j,cards = %j", room.rid(), preCards, this._prehandcards);

    //     return preCards;
    // },
});